<?php
/**
 * User: @lose
 * Date: 2019-10-10
 * Time: 18:02
 */

namespace Biz\service\admin;

use Biz\model\store\AdWhiteList as mAdWhiteList;
use Doraemon\service\User;

class AdWhiteList
{
    public static function getList($where, $limit)
    {
        $list = mAdWhiteList::getListByAdmin($where, $limit);
        $shopIds = array_column($list, 'shop_id');
        if($shopIds) {
            $batchUserInfo = User::getBatchBasicUserInfo($shopIds);
            $batchUserInfo = array_column($batchUserInfo, null, 'uid');
            foreach ($list as &$info) {
                if (isset($batchUserInfo[$info['shop_id']])) {
                   $info['shop_name'] = $batchUserInfo[$info['shop_id']]['nick'];
                }
            }
        }

        return ['list' => $list, 'total' => mAdWhiteList::getCount($where)];
    }

    public static function update($id, $params, $adminId)
    {
        $data = [
            'shop_id' => $params['shop_id'],
            'start_time' => $params['start_time'],
            'end_time' => $params['end_time'],
            'remark' => $params['remark'],
            'edittime' => date('Y-m-d H:i:s'),
            'whitelist_status' => mAdWhiteList::STATUS_NORMAL,
            'admin_id' => $adminId
        ];

        if($id > 0) {
            return mAdWhiteList::updateById($id, $data);
        } else {
            $data['addtime'] = date('Y-m-d H:i:s');
            return mAdWhiteList::add($data);
        }
    }

    public static function getInfo($id)
    {
        $info = mAdWhiteList::getInfo($id);
        $userInfo = User::getBasicUserInfo($info['shop_id']);
        $info['shop_name'] = $userInfo['nick'];
        return $info;
    }
}